package hc.appt.action;

import hc.appt.ApptDTO;
import hc.appt.service.ApptService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AppointmentServlet
 */
@WebServlet("/AppointmentServlet")
public class AppointmentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private ApptService as = new ApptService();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AppointmentServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("----do get-------------");
		System.out.println("userID=" + request.getParameter("userId"));
		String res = this.getAppt(request.getParameter("userId"));
		System.out.println("res=" + res);
		System.out.println("----do get----finished---------");
		response.getWriter().write(res);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("---do post-----");
		System.out.println("userID=" + request.getParameter("userId"));
		String res = this.getAppt(request.getParameter("userId"));
		System.out.println("res=" + res);
		System.out.println("---do post---finish--");
		response.getWriter().write(res);
	}

	private String getAppt(String parameter) {
		ArrayList<ApptDTO> apptList = new ArrayList<>();
		apptList = as.getApptList(parameter);
		String rsHtml = this.getResponseTable(apptList);
		return rsHtml;
	}

	private String getResponseTable(ArrayList<ApptDTO> apptList) {
		// StringBuffer table = new StringBuffer(
		// "<table id='apptListId' border='0' rules=none cellspacing=0 width='100%' style='color:#BE81F7'>"
		// +
		// "<tr align='left' bgcolor='#76C2F8'><th>AppID</th><th>Date</th><th>Doctor</th><th>Patient</th><th>Status</th><th>Hospital</th>"
		// + "</tr>");

		StringBuffer table = new StringBuffer("<table id='aListId'>" + "<tr>"
				+ "<th>AppID</th>" + "<th>Date</th>" + "<th>Doctor</th>"
				+ "<th>Patient</th>" + "<th>Status</th>" + "<th>Hospital</th>"
				+ "</tr>");
		StringBuffer trStart = new StringBuffer();

		for (ApptDTO appt : apptList) {
			StringBuffer td = new StringBuffer();
			trStart.append("<tr id='appt-" + appt.getAppt().getApptId()
					+ "' onclick=mgrAppt(" + appt.getAppt().getApptId()
					+ ")>");
			td.append("<td>");
			td.append(appt.getAppt().getApptId());
			td.append("</td>");

			td.append("<td>");
			td.append(appt.getAppt().getDate());
			td.append("</td>");

			td.append("<td>");
			td.append(appt.getDoctor().getFullName());
			td.append("</td>");

			td.append("<td>");
			td.append(appt.getPatient().getFullName());
			td.append("</td>");

			td.append("<td>");
			td.append(appt.getAppt().getStatus());
			td.append("</td>");

			td.append("<td>");
			td.append(appt.getHospital().getName());
			td.append("</td>");

			trStart.append(td);
			trStart.append("</tr>");
		}

		table.append(trStart);
		table.append("</table>");
		return table.toString();
	}
}
